package xworker.dataObject.db.dialets;

import xworker.dataObject.PageInfo;
import xworker.dataObject.db.SqlDialet;
import xworker.dataObject.query.ConditionValue;
import xworker.dataObject.utils.PageType;

import java.util.List;

public class DerbyDialet implements SqlDialet {
    @Override
    public String getCountSql(String sql) {
        return "select count(1) from (" + sql + ") as t";
    }

    @Override
    public String getPageSql(String sql, List<ConditionValue> valueList, PageInfo pageInfo) {
        if(pageInfo != null && pageInfo.getLimit() > 0) {
            sql = "SELECT * FROM ( SELECT ROW_NUMBER() OVER() AS rownum, t.*  FROM (" + sql + " ) t) AS tmp WHERE rownum >= ? AND rownum <= ?";

            long start = pageInfo.getStart() + 1;
            long end = start + pageInfo.getLimit() - 1;
            valueList.add(new ConditionValue(start, "long"));
            valueList.add(new ConditionValue(end, "long"));
        }

        return sql;
    }

    @Override
    public PageType getPageType() {
        return PageType.BETWEEN;
    }
}
